package com.dcbeida.activity.inventory;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.CaptureActivity;
import com.dcbeida.bean.DrugInfo;
import com.dcbeida.utils.Constants;
import com.dcbeida.utils.Utils;

/**
 * 盘点
 */
public class InventoryActivity extends BaseActivity implements View.OnClickListener, InventoryContract.View {


    @Bind(R.id.img_back)
    ImageView img_back;


    @Bind(R.id.tv_type)
    TextView tv_type;

    @Bind(R.id.tv_all2)
    TextView tv_all2;

    @Bind(R.id.lin_add)
    LinearLayout lin_add;

    @Bind(R.id.lin_confim)
    LinearLayout lin_confim;

    @Bind(R.id.tv_confim)
    TextView tv_confim;

    @Bind(R.id.tvAddCode)
    TextView tvAddCode;


    private InventoryContract.Present mPresent;

    LayoutInflater inflater;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_inventory;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initListeners();
        initParams();
        inflater = LayoutInflater.from(this);
        mPresent.getAllInventoryCount();
    }

    private void initListeners() {
        img_back.setOnClickListener(this);
        lin_confim.setOnClickListener(this);
        tv_confim.setOnClickListener(this);

    }

    private void initParams() {
        mPresent = new InventoryPresenter(this, this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.lin_confim:
                Bundle b = new Bundle();
                b.putInt("type", CaptureActivity.TYPE_RE);
                showActivityForResult(CaptureActivity.class, b, Constants.RESULT_CAPTER);
                break;
            case R.id.tv_confim:

                if(mPresent.DrugIsEmpty()){
                    showTip("请先盘点药品再提交");
                    return;
                }
                new AlertDialog.Builder(this).setMessage(R.string.message)
                        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).setPositiveButton(R.string.confim, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        mPresent.addInventory();
                    }
                }).show();
                break;
        }
    }

    @Override
    public void setCount(int count) {
        // tv_all.setText(count + "");

    }

    @Override
    public void showTip(String msg) {
        if (msg.equalsIgnoreCase("ok")) {

            super.showTip(getString(R.string.sccg));
        } else {
            super.showTip(msg);
        }
    }

    @Override
    public void showP() {

    }


//    @Bind(R.id.tv_name_numb)
//    TextView tv_name_numb;
//
//    @Bind(R.id.tv_itemAll)
//    TextView tv_itemAll;


    @Override
    public void addItem(DrugInfo info) {

        View view = inflater.inflate(R.layout.activity_inventory_item, null);
        TextView tv_name_numb = ButterKnife.findById(view, R.id.tv_name_numb);
        TextView tv_itemAll = ButterKnife.findById(view, R.id.tv_itemAll);
        tv_name_numb.setText(getString(R.string.name_numb, info.getDrugname(), info.getDrugcode()));
        tv_itemAll.setText(info.getQuantity() + "");
        lin_add.addView(view);


    }

    @Override
    public void setQuantity(int count, int quantity) {
        if (quantity > 0) {
            tvAddCode.setText(R.string.again333);
        } else {
            tvAddCode.setText(R.string.again);
        }

        tv_type.setText(count + "");
        tv_all2.setText(quantity + "");
    }

    @Override
    public void confimSuccess() {
        sendBroadcast(new Intent(Constants.RECEIVER_STOCK_FRESH));
        finish();
    }

    @Override
    public void addItem(int pos, DrugInfo info, int head_pos, DrugInfo headInfo) {

        LinearLayout itemView = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.inventory_head_item, null);
        LinearLayout lin_item_head = (LinearLayout) itemView.findViewById(R.id.lin_item_head);

        TextView tv_xt_num = (TextView) itemView.findViewById(R.id.tv_xt_num);
        TextView tv_yp_name = (TextView) itemView.findViewById(R.id.tv_name);
        TextView tv_count = (TextView) itemView.findViewById(R.id.tv_num);
        TextView tv_ph = (TextView) itemView.findViewById(R.id.tv_ph);
        TextView tv_yxq = (TextView) itemView.findViewById(R.id.tv_yxq);
        TextView tv_bh = (TextView) itemView.findViewById(R.id.tv_bh);
        TextView tv_box = (TextView) itemView.findViewById(R.id.tv_box);

        View headView = lin_add.getChildAt(head_pos);
        if (headView != null) {
            TextView tv_count_head = (TextView) headView.findViewById(R.id.tv_num);
            tv_count_head.setText(headInfo.getCount() + "");
        } else {
            tv_count.setText(headInfo.getCount() + "");
        }

        tv_yxq.setText(info.getExpiredate());
        tv_bh.setText(info.getDrugcode());
        tv_box.setText(info.getQuantity() + "");
        tv_ph.setText(info.getBatchnumber());
        tv_yp_name.setText(headInfo.getDrugname());
        tv_xt_num.setText(info.getStock()+"");

        if (info.isFirst()) {
            lin_item_head.setVisibility(View.VISIBLE);
        } else
            lin_item_head.setVisibility(View.GONE);

        lin_add.addView(itemView, pos);

        tvAddCode.setText(R.string.again333);
    }

    @Override
    public void setPresenter(InventoryContract.Present presenter) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i(InventoryActivity.class.getName(), "#########requestCode:" + requestCode + ";######resultCode:" + resultCode);
        if (resultCode == RESULT_OK && requestCode == Constants.RESULT_CAPTER) {
            String code = data.getStringExtra("code");
            Log.i(InventoryActivity.class.getName(), "---" + code);
            mPresent.addinventorydrugcodes(code);
        }
    }
}
